//页面加载完毕
$(function () {

    var studentChangeClass_datagrid = $("#studentChangeClass_datagrid");
    var studentChangeClass_dialog = $("#studentChangeClass_dialog");
    var studentChangeClass_form = $("#studentChangeClass_form");

    //学员班级转换列表初始化
    studentChangeClass_datagrid.datagrid({
        url:"/studentChangeClass/list.do",
        fit:true,
        fitColumns:true,
        striped:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        toolbar:"#tb",
        columns:[[
            {field:"id", hidden:true},
            {field:"name", title:"学员", width:80,align:'center'},
            {field:"totalTuition", title:"总学费", width:80,align:'center',formatter:totalTuition},
            {field:"hasTuition", title:"已交款", width:80,align:'center'},
            {field:"noTuition", title:"未交款", width:80,align:'center'},
            {field:"changeClassTime", title:"转班时间", width:80,align:'center'},
            {field:"tel", title:"电话", width:80,align:'center'},
            {field:"beforeClass", title:"以前班级", width:80,formatter:beforeClass,align:'center'},
            {field:"nowClass", title:"现在班级", width:80,align:'center',formatter:nowClass},
            {field:"marketingPerson", title:"营销人员", width:80,formatter:marketingPerson,align:'center'},
            {field:"auditState", title:"审核状态", width:80,formatter:auditState,align:'center'}
        ]]
    });
    //end

    var cmdObj = {
        //查看操作
        edit : function () {
            //判断是否选中
            var row = studentChangeClass_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条查看数据", "info");
                return;
            }

            studentChangeClass_dialog.dialog("open");

            //修改对话框标题
            studentChangeClass_dialog.dialog("setTitle","编辑学员班级转换");

            //总学费
            if(row.totalTuition){
                row["totalTuition.id"] = row.totalTuition.id;
            }
            //营销人员
            if(row.marketingPerson){
                row["marketingPerson.id"] = row.marketingPerson.id;
            }
            //以前班级
            if(row.beforeClass){
                row["beforeClass.id"] = row.beforeClass.id;
            }
            //现在班级
            if(row.nowClass){
                row["nowClass.id"] = row.nowClass.id;
            }
            //数据回显
            studentChangeClass_form.form("load",row);
            //设置只读
            $(".readOn").textbox('readonly',true);

        },
        //审核
        audit : function () {
            //判断是否选中
            var row = studentChangeClass_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示','是否确认审核?',function(ret){
                if (ret){
                    //确定审核
                    $.get("/studentChangeClass/audit.do?id=" + row.id,function (data) {
                        if(data.success){
                            //审核成功
                            $.messager.confirm('温馨提示','审核成功!',function(ret){
                                //刷新数据
                                studentChangeClass_datagrid.datagrid("reload");
                            });

                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //高级查询  思路: 加载学员班级转换列表时  将多条件查询数据一同发起请求
        searchT : function () {
            var keyword = $("#keyword").val();
            var clazzId = $("#clazzId").val();
            var auditStatus = $("#auditStatus option:selected").val();
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();
            //加载数据列表时 携带额外参数
            studentChangeClass_datagrid.datagrid("load",{
                keyword:keyword ,
                clazzId:clazzId ,
                auditStatus:auditStatus ,
                beginDate:beginDate ,
                endDate:endDate
            })
        },
        //取消操作
        cancel : function (){
            studentChangeClass_dialog.dialog("close");
        },
        //刷新
        reload : function (){
            studentChangeClass_datagrid.datagrid("reload");
        }
    };

    //调用
    //给所有的按钮统一绑定事件
    $("a[data-cmd]").on("click",function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    //对话框初始化
    studentChangeClass_dialog.dialog({
        width:320,
        height:380,
        buttons:"#bb",
        closable:true,
        closed:true
    });
    //end

});

//总学费格式化
function totalTuition(value, row, index){
    return value ? value.name : "";
}

//审核状态格式化
function auditState(value, row, index){
    if(value == 0){
        return "<span style='color: red'>未审核</span>"
    }
    return "<span style='color: #00ee00;'>已审核</span>";
}
//以前班级格式化
function beforeClass(value, row, index){
    return value ? value.name : "";
}

//现在班级格式化
function nowClass(value, row, index){
    return value ? value.name : "";
}
//营销人员
function marketingPerson(value, row, index){
    return value ? value.username : "";
}
